<?php
namespace App\Modules\Admin\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use App\Modules\Admin\Services\AdminUserService;

class AdminUserController extends BaseController
{

    public function __construct(AdminUserService $adminUserService)
    {
        $this->middleware('check.adminLogin')->except('index');

        $this->adminUserService = $adminUserService;
    }

    public function index()
    {
        $param = [];
        $data = $this->adminUserService->getList($param);

        return $this->response->success($data);
    }

    public function show(Request $request)
    {
        $adminUser = $request->user();
        $adminUser->admin_uid = (string) $adminUser->admin_uid;
        return $this->response->success($adminUser);

    }

    public function store(Request $request)
    {
        $param = $request->only(['account', 'password', 'name', 'sex']);
        $result = $this->adminUserService->add($param);

        return $this->response->success($result);
    }

    public function update(Request $request)
    {
        $param = $request->only(['account', 'password', 'name', 'sex', 'admin_uid']);

        $result = $this->adminUserService->update($param);

        return $this->response->success($result);
    }

    public function destroy(Request $request)
    {
        $id = $request->input('id');
        $result = $this->adminUserService->delete($id);

        return response()->json($result);
    }

    public function updatePassword(Request $request)
    {
        $param = $request->only(['old_password', 'new_password', 'admin_uid']);

        $result = $this->adminUserService->updatePassword($param);

        return $this->response->success($result);

    }
}
